package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"study_abroad_system_back_end/common/StatusCode"
	"study_abroad_system_back_end/dao"
	"study_abroad_system_back_end/models"
	"study_abroad_system_back_end/response"
	"study_abroad_system_back_end/serializer"
)

func CollegeList(ctx *gin.Context) {
	code := StatusCode.SUCCESS

	// 数据库院校总条数
	DB := dao.GetDB()
	var c models.College
	result := DB.Find(&c)
	totalNum := result.RowsAffected

	// ps := ctx.PostForm("pageSize") //页面大小
	// pn := ctx.PostForm("pageNum")  //当前页码
	// pageSize, _ := strconv.Atoi(ps)
	// pageNum, _ := strconv.Atoi(pn)
	//这个写法会报错，我又改回来了
	data := make(map[string]interface{})
	ctx.BindJSON(&data)
	ps := data["pageSize"] //页面大小
	pn := data["pageNum"]  //当前页码
	
	pageSize := int(ps.(float64))
	pageNum := int(pn.(float64))

	var colleges []models.College
	offset := (pageNum - 1) * pageSize
	limit := pageSize

	DB.Limit(limit).Offset(offset).Find(&colleges)
	response.Response(ctx, http.StatusOK, code, gin.H{"pageNum": pageNum, "totalNum": totalNum, "collegeList": serializer.BuildCollegesList(colleges)}, StatusCode.GetMsg(code))
}

func CollegeDetail(ctx *gin.Context) {
	s := ctx.Param("id")
	id, _ := strconv.Atoi(s)
	var college models.College
	code := StatusCode.SUCCESS

	DB := dao.GetDB()
	DB.First(&college, id)
	//如果学校id不存在，返回出错信息
	if college.Id == 0 {
		code = StatusCode.ERROR_NOT_EXIST_COLLEGE
		response.Response(ctx, http.StatusOK, code, nil, StatusCode.GetMsg(code))
		return
	}
	response.Response(ctx, http.StatusOK, code, gin.H{"college": serializer.BuildCollege(college)}, StatusCode.GetMsg(code))
}

func CollegeRank(ctx *gin.Context) {
	code := StatusCode.SUCCESS
	DB := dao.GetDB()
	var colleges []models.College
	s := ctx.Param("id")
	id, _ := strconv.Atoi(s)

	var rank []models.College
	result := DB.Find(&colleges)

	if id==0{
		rank = Sort(colleges, 1)
		response.Response(ctx,http.StatusOK,code,gin.H{"Engineering":serializer.BuildCollegesRank0(rank)},StatusCode.GetMsg(code))
	}
	if id==1{
		rank = Sort(colleges, 2)
		response.Response(ctx,http.StatusOK,code,gin.H{"Medical":serializer.BuildCollegesRank1(rank)},StatusCode.GetMsg(code))
	}
	if id==2{
		rank = Sort(colleges, 3)
		response.Response(ctx,http.StatusOK,code,gin.H{"Social":serializer.BuildCollegesRank2(rank)},StatusCode.GetMsg(code))
	}
	if id==3{
		rank = Sort(colleges, 4)
		response.Response(ctx,http.StatusOK,code,gin.H{"Bioscience":serializer.BuildCollegesRank3(rank)},StatusCode.GetMsg(code))
	}
	if id==4{
		rank = Sort(colleges, 5)
		response.Response(ctx,http.StatusOK,code,gin.H{"Physical":serializer.BuildCollegesRank4(rank)},StatusCode.GetMsg(code))
	}
	if id==5{
		rank = Sort(colleges, 6)
		response.Response(ctx,http.StatusOK,code,gin.H{"Humanities":serializer.BuildCollegesRank5(rank)},StatusCode.GetMsg(code))
	}
	_ = result.RowsAffected
	//var rank2 []models.College
	//var rank3 []models.College
	//var rank4 []models.College
	//var rank5 []models.College
	//var rank6 []models.College

	//rank = Sort(colleges, 1)
	//response.Response(ctx,http.StatusOK,code,gin.H{"工程技术排名":serializer.BuildCollegesList(rank)},StatusCode.GetMsg(code))
	//rank = Sort(colleges, 2)
	//response.Response(ctx,http.StatusOK,code,gin.H{"医学保健排名":serializer.BuildCollegesList(rank)},StatusCode.GetMsg(code))
	//rank = Sort(colleges, 3)
	//response.Response(ctx,http.StatusOK,code,gin.H{"社会科学排名":serializer.BuildCollegesList(rank)},StatusCode.GetMsg(code))
	//rank = Sort(colleges, 4)
	//response.Response(ctx,http.StatusOK,code,gin.H{"生命科学排名":serializer.BuildCollegesList(rank)},StatusCode.GetMsg(code))
	//rank = Sort(colleges, 5)
	//response.Response(ctx,http.StatusOK,code,gin.H{"物理科学排名":serializer.BuildCollegesList(rank)},StatusCode.GetMsg(code))
	//rank = Sort(colleges, 6)
	//response.Response(ctx,http.StatusOK,code,gin.H{"人文学科排名":serializer.BuildCollegesList(rank)},StatusCode.GetMsg(code))


	//response.Response(ctx, http.StatusOK, code, gin.H{
	//	"工程技术排名": serializer.BuildCollegesList(rank1),
	//	"医学保健排名": serializer.BuildCollegesList(rank2),
	//	"社会科学排名": serializer.BuildCollegesList(rank3),
	//	"生命科学排名": serializer.BuildCollegesList(rank4),
	//	"物理科学排名": serializer.BuildCollegesList(rank5),
	//	"人文学科排名": serializer.BuildCollegesList(rank6),
	//},
	//	StatusCode.GetMsg(code))

}

func Sort(array []models.College, num int) []models.College {
	//var ret []models.College
	//ret = array
	if num == 1 {
		min := 1000
		loc := -1
		for i := 0; i < len(array); i++ {
			for j := i; j < len(array); j++ {
				if array[j].Rank1 < min && array[j].Rank1 > 0 {
					min = array[j].Rank1
					loc = j
				}
			}
			if loc == -1 {
				break
			}
			tmp := array[i]
			array[i] = array[loc]
			array[loc] = tmp
			loc = -1
			min = 1000
		}
	}
	if num == 2 {
		min := 1000
		loc := -1
		for i := 0; i < len(array); i++ {
			for j := i; j < len(array); j++ {
				if array[j].Rank2 < min && array[j].Rank2 > 0 {
					min = array[j].Rank2
					loc = j
				}
			}
			if loc == -1 {
				break
			}
			tmp := array[i]
			array[i] = array[loc]
			array[loc] = tmp
			loc = -1
			min = 1000
		}
	}
	if num == 3 {
		min := 1000
		loc := -1
		for i := 0; i < len(array); i++ {
			for j := i; j < len(array); j++ {
				if array[j].Rank3 < min && array[j].Rank3 > 0 {
					min = array[j].Rank3
					loc = j
				}
			}
			if loc == -1 {
				break
			}
			tmp := array[i]
			array[i] = array[loc]
			array[loc] = tmp
			loc = -1
			min = 1000
		}
	}
	if num == 4 {
		min := 1000
		loc := -1
		for i := 0; i < len(array); i++ {
			for j := i; j < len(array); j++ {
				if array[j].Rank4 < min && array[j].Rank4 > 0 {
					min = array[j].Rank4
					loc = j
				}
			}
			if loc == -1 {
				break
			}
			tmp := array[i]
			array[i] = array[loc]
			array[loc] = tmp
			loc = -1
			min = 1000
		}
	}
	if num == 5 {
		min := 1000
		loc := -1
		for i := 0; i < len(array); i++ {
			for j := i; j < len(array); j++ {
				if array[j].Rank5 < min && array[j].Rank5 > 0 {
					min = array[j].Rank5
					loc = j
				}
			}
			if loc == -1 {
				break
			}
			tmp := array[i]
			array[i] = array[loc]
			array[loc] = tmp
			loc = -1
			min = 1000
		}
	}
	if num == 6 {
		min := 1000
		loc := -1
		for i := 0; i < len(array); i++ {
			for j := i; j < len(array); j++ {
				if array[j].Rank6 < min && array[j].Rank6 > 0 {
					min = array[j].Rank6
					loc = j
				}
			}
			if loc == -1 {
				break
			}
			tmp := array[i]
			array[i] = array[loc]
			array[loc] = tmp
			loc = -1
			min = 1000
		}
	}

	return array

}
